package net.xju3_4.dao;

import net.xju3_4.common.utils.JdbcUtilsV1;
import net.xju3_4.common.utils.PageInfo;
import net.xju3_4.entity.Orders;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class OrdersDao {
    public List<Orders> list() {
        // 操作数据库
        // 获取连接
        Connection connection = JdbcUtilsV1.getConnection();
        String sql = "select * from orders";
        List<Orders> ordersList = JdbcUtilsV1.executeQuery(connection, Orders.class, sql);
        return ordersList;
    }

    public void insert(Orders orders) {
        String sql = "INSERT INTO orders(order_Number,typeid,customer_id,start_Time,end_Time,route_Description," +
                "total_Distance,status,total_Amount,payment_Status,created_At) " +
                "VALUES(?,?,?,?,?,?,?,?,?,?,?)";
        JdbcUtilsV1.executeUpdate(sql, orders.getOrderNumber(),
                orders.getTypeid(),orders.getCustomerid(),orders.getStartTime(),orders.getEndTime(),orders.getRouteDescription(),
                orders.getTotalDistance(),orders.getStatus(),orders.getTotalAmount(),orders.getPaymentStatus(),
                orders.getCreatedAt());
    }

    public Orders selectOne(int id) {
        String sql = "select id,order_Number orderNumber,typeid,customer_id customerid,start_Time startTime,end_Time endTime,route_Description routeDescription," +
                " total_Distance totalDistance,status ,total_Amount totalAmount  ,payment_Status paymentStatus,created_At createdAt" +
                " from orders where id=?";
        return JdbcUtilsV1.executeQueryOne(JdbcUtilsV1.getConnection(), Orders.class, sql, id);
    }

    public void deleteById(int id) {
        // 逻辑删除，实际执行update
        String sql = "delete from orders where id = ?";
        JdbcUtilsV1.executeUpdate(sql, id);
    }

    public void update(Orders orders) {
        StringBuilder sql = new StringBuilder("UPDATE orders SET ");
        List<Object> params = new ArrayList<>();

        // 动态拼接非空字段
        if (orders.getOrderNumber() != null &&!orders.getOrderNumber().isEmpty()) {
            sql.append( "order_Number = ?, ");
            params.add(orders.getOrderNumber());
        }

      
        if (orders.getCustomerid() != null &&orders.getCustomerid() != 0) {
            sql.append( "customer_id = ?, ");
            params.add(orders.getCustomerid());
        }
        if (orders.getStartTime() != null) {
            sql.append( "start_Time = ?, ");
            params.add(orders.getStartTime());
        }
        if (orders.getEndTime() != null) {
            sql.append( "end_Time = ?, ");
            params.add(orders.getEndTime());
        }
        if (orders.getRouteDescription() != null&&!orders.getRouteDescription().isEmpty()) {
            sql.append( "route_Description = ?, ");
            params.add(orders.getRouteDescription());
        }
        if (orders.getTotalAmount() != null&&orders.getTotalAmount() != 0.0) {
            sql.append( "total_Distance = ?, ");
            params.add(orders.getTotalAmount());
        }
        if (orders.getStatus() != null&&!orders.getStatus().isEmpty()) {
            sql.append( "status = ?, ");
            params.add(orders.getStatus());
        }
        if (orders.getTotalAmount() != null&&orders.getTotalAmount() != 0.0) {
            sql.append( "total_Amount = ?, ");
            params.add(orders.getTotalAmount());
        }
        if (orders.getPaymentStatus() != null&&!orders.getPaymentStatus().isEmpty()) {
            sql.append( "payment_Status = ?, ");
            params.add(orders.getPaymentStatus());
        }
        if (orders.getCreatedAt() != null) {
            sql.append( "created_At = ?, ");
            params.add(orders.getCreatedAt());
        }
        // 移除最后的逗号和空格
        if (!params.isEmpty()) {
            sql.delete(sql.length() - 2, sql.length());
        }
        // 添加WHERE条件
        sql.append(" WHERE id = ?");
        params.add(orders.getId());
        JdbcUtilsV1.executeUpdate(sql.toString(),params.toArray());
    }

    public PageInfo<Orders> pageList(String orderNumber, String typeid, String customerid,Integer pageNo, Integer pageSize) {
        String sql = "select id,order_Number orderNumber,typeid,customer_id customerid,start_Time startTime,end_Time endTime,route_Description routeDescription," +
                " total_Distance totalDistance,status ,total_Amount totalAmount  ,payment_Status paymentStatus,created_At createdAt" +
                " from orders where 1 = 1 ";
        List<Object> params = new ArrayList<Object>();
        // 拼接sql 条件查询
        if (orderNumber != null && !orderNumber.isEmpty()) {
            sql += " and order_Number like ?";
            params.add("%" + orderNumber + "%");
        }
        if (typeid != null && !typeid.isEmpty()) {
            sql += " and typeid = ?";
            params.add(typeid);
        }
        if (customerid != null && !customerid.isEmpty()) {
            sql += " and customer_id = ?";
            params.add(customerid);
        }
        sql += " limit ?,?";
        params.add((pageNo-1)*pageSize);
        params.add(pageSize);
        List<Orders> orderss = JdbcUtilsV1.executeQuery(Orders.class, sql, params.toArray());
        // 分页
        String countSql = "select count(*) from orders where 1 = 1 ";
        long count = JdbcUtilsV1.executeQueryOneValue(countSql);
        PageInfo<Orders> pageInfo = new PageInfo<>(pageNo, pageSize, (int) count, orderss);
        return pageInfo;
    }
}
